﻿using BeerXMLWPF.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BeerXMLWPF
{
    /// <summary>
    /// Interaction logic for AddFermentable.xaml
    /// </summary>
    public partial class AddFermentable : Window
    {
        List<string> lstCombobox;
        public AddFermentable()
        {
            InitializeComponent();
            lstCombobox = new List<string>();
            lstCombobox.Add("Grain");
            lstCombobox.Add("Sugar");
            lstCombobox.Add("Extract");
            lstCombobox.Add("Dry Extract");
            lstCombobox.Add("Adjunct");
            cbType.ItemsSource = lstCombobox;
            LoadContent("new");
        }
        public AddFermentable(string s)
        {
            InitializeComponent();
            lstCombobox = new List<string>();
            lstCombobox.Add("Grain");
            lstCombobox.Add("Sugar");
            lstCombobox.Add("Extract");
            lstCombobox.Add("Dry Extract");
            lstCombobox.Add("Adjunct");
            cbType.ItemsSource = lstCombobox;
            LoadContent(s);
        }

        private void LoadContent(string s)
        {
            if (s == "new")
            {
                Global.fermentable = new Fermentable();
                cbType.SelectedItem = "Grain";
            }
            else
            {
                txtName.Text = Global.fermentable.Name;
                txtYeild.Text = Global.fermentable.Yield;
                txtAmount.Text = Global.fermentable.Amount;
                cbType.SelectedItem = Global.fermentable.Type;
                txtColor.Text = Global.fermentable.Color;
            }

        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (txtName.Text == "")
            {
                MessageBox.Show("Please enter fermentable name");
                return;
            }
            try
            {
                double amount = double.Parse(txtAmount.Text);
                if (amount <= 0)
                {
                    MessageBox.Show("Amount must be a double and greater than 0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Amount must be a double and greater than 0");
                return;
            }
            try
            {
                double time = double.Parse(txtYeild.Text);
                if (time <= 0 || time >= 100)
                {
                    MessageBox.Show("Yeild must be a integer and greater than 0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Yeild must be a integer and greater than 0");
                return;
            }
            try
            {
                double alpha = double.Parse(txtColor.Text);
                if (alpha <= 0)
                {
                    MessageBox.Show("Color must be a double and in bettween 0 and 100");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Color must be a double and in bettween 0 and 100");
                return;
            }
            
            Global.fermentable.Name = txtName.Text;
            Global.fermentable.Yield = double.Parse(txtYeild.Text).ToString();
            Global.fermentable.Amount = double.Parse(txtAmount.Text).ToString();
            Global.fermentable.Display_Amount = Global.fermentable.Amount + " kg";
            Global.fermentable.Color = double.Parse(txtColor.Text).ToString();
            Global.fermentable.Display_Color = Global.fermentable.Color + "SRM";
            Global.fermentable.Type = cbType.SelectedItem.ToString();
            Global.fermentable.Version = "1";
            Global.OK = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
